<?php


namespace App\Console\Commands;

use App\Models\CommissionLog;
use App\Models\MiyouDaily;
use App\Models\MiyouUser;
use App\Models\Users;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class GenerateData extends Command
{
    /**
     * 命令行执行命令
     * @var string
     */
    protected $signature = 'generate:data {date=today}';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $date = $this->argument('date');

        $today = date('Y-m-d');
        do {
            $this->oneDay($date);
            $date = date('Y-m-d', strtotime($date) + 86400);
        } while($date <= $today);
        return true;
    }

    protected function oneDay($date){
        $timestamp = strtotime($date);
        if(empty($timestamp)) {
            return false;
        }
        $timeStr = date('Y-m-d 00:00:00', $timestamp);
        while (true) {
            $user = Users::query()
                ->where('gen_time', '<', $timeStr)
                ->orderBy('id', 'desc')->limit(1)->first();
            if(empty($user)) {
                break;
            }
            $this->oneUser($user, $timestamp);
        }
    }

    protected function oneUser($user, $timestamp){
        $miyouId = MiyouUser::getMiyouId($user->mobile);
        $myAchievement = 0;
        $myTdAchievement = 0;
        if($miyouId) {
            $myAchievement = MiyouDaily::query()->where('miyou_id', $miyouId)->sum('achievement');
            $myTdAchievement  = MiyouDaily::query()->where('miyou_id', $miyouId)
                ->where('calc_date', date('Y-m-d', $timestamp))
                ->sum('achievement');
        }
        $directUser = Users::query()->where('pid', $user->id)->get();
        $allUser = $directUser->count();
        $achievement = 0;
        $tdAchievement = 0;
        foreach ($directUser as $u) {
            $achievement += $u->achievement + $u->my_achievement;;
            $tdAchievement  += $u->td_achievement + $u->my_td_achievement;
            $allUser += $u->all_user;
        }
        $userRate = Users::getRewardRate($achievement, $allUser, $user->reward_rate);
        $commission = 0;
        $parentCommission = 0;
        if($userRate > 0){
            foreach ($directUser as $u) {
                //$childRate = $this->getCommissionRate($u->achievement, $u->all_user);
                //$diffRate = $userRate - $childRate;
                $commission += $userRate * $u->my_td_achievement;
                $parentCommission += 0.01 * $u->my_td_achievement;
            }
        }
        $achievement = round($achievement, 2);
        $tdAchievement = round($tdAchievement, 2);
        $now = date('Y-m-d H:i:s');
        $updateArr = [
            'commission' => $commission,
            'all_user' => $allUser,
            'achievement' => $achievement,
            'td_achievement' => $tdAchievement ,
            'my_achievement' => $myAchievement,
            'my_td_achievement' => $myTdAchievement ,
            'gen_time' => date('Y-m-d', $timestamp),
        ];
        $updateArr['commission'] += $user->commission;
        Users::query()->where('id', $user->id)->update($updateArr);
        $tag = date('Ymd', $timestamp);
        if($commission > 0) {
            CommissionLog::query()->insert([
                'user_id' =>  $user->id,
                'amount' =>  $commission,
                'type' =>  CommissionLog::TYPE_IN,
                'create_time' => $now,
                'tag' => implode("|", [
                     $tdAchievement, $achievement
                ]),
            ]);
        }

        if($parentCommission > 0 && $user->pid > 0) {
            Users::query()->where('id', $user->pid)->increment('commission', $parentCommission);
            CommissionLog::query()->insert([
                'user_id' =>  $user->pid,
                'amount' =>  $parentCommission,
                'type' =>  CommissionLog::TYPE_IN,
                'create_time' => $now,
                'tag' => implode("|", [
                    $tdAchievement, $achievement
                ]),
            ]);
        }
    }


}